//
//  CLSettingViewController.m
//  chanlin
//
//  Created by Ryan on 14-12-21.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "CLSettingViewController.h"
#import "BlockUI.h"
#import "CLGlobleDataModel.h"
#import "INHtmlElementUtil.h"
#import "SDImageCache.h"
#import "SgrGCD.h"
#import "CLWebVeiwController.h"
#import "CLPushAnimatedRight.h"
#import "CLSlidersViewController.h"
#import "CLAboutViewController.h"
#import <Foundation/Foundation.h>
#import "UIView+Utils.h"
#import "CLDocModel.h"

@interface CLSettingViewController ()
@property (nonatomic,strong)UIButton *backButton;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIImageView *fontSeleceted;
@property (nonatomic,weak) UILabel *label2;
@property (nonatomic,strong)UIActivityIndicatorView *waittingView;
@property (nonatomic,strong)UISlider *fontSlider;

@property (nonatomic, strong) UIImageView *fontPointImg;

@end

@implementation CLSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.fontSeleceted=[[UIImageView alloc] init];
    self.fontSeleceted.image=[UIImage imageNamed:@"set_selected.png"];
    self.fontSeleceted.frame=CGRectMake(270, (50-22)/2.f, 33, 22.5);
    
    _fontPointImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 18, 18)];
    [_fontPointImg setImage:[UIImage imageNamed:@"setting_fontPoint_icon.png"]];
    _fontPointImg.backgroundColor = [UIColor clearColor];
    _fontPointImg.userInteractionEnabled=YES;
    
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] init];
    [pan addTarget:self action:@selector(panFont:)];
    [_fontPointImg addGestureRecognizer:pan];
    
    self.view.backgroundColor=[UIColor whiteColor];
    self.backButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.backButton setImage:[UIImage imageNamed:@"tb_back.png"] forState:UIControlStateNormal];
    self.backButton.contentEdgeInsets=UIEdgeInsetsMake(7, 7, 7, 7);

    self.backButton.frame=CGRectMake(15.f, 20.f, 44.f, 44.f);
    __weak typeof(self) me=self;
    [self.backButton handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [me.dispatchObj pop];
    }];
    [self.view addSubview:self.backButton];
    
    
    UILabel *title=[[UILabel alloc] init];
    title.font=[UIFont systemFontOfSize:20];
    title.frame=CGRectMake((GlobleWidth-100.f)/2.f, 30.f, 100.f, title.font.lineHeight);
    title.textColor=RGB(98, 101, 113);
    title.textAlignment=NSTextAlignmentCenter;
    title.text=@"设置";
    
    [self.view addSubview:title];
    
    CALayer *line=[CALayer layer];
    line.backgroundColor=title.textColor.CGColor;
    line.frame=CGRectMake(0, 64.f, GlobleWidth, 0.5);
    [self.view.layer addSublayer:line];

    
    
    self.tableView=[[UITableView alloc] init];
    self.tableView.frame=CGRectMake(0, 64, GlobleWidth, GlobleHeight-64);
    
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.scrollsToTop=NO;
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor=[UIColor whiteColor];
    UIView *bg=[[UIView alloc] init];
    self.tableView.backgroundView=bg;
    bg.backgroundColor=RGB(232, 233, 232);
    self.tableView.backgroundView=bg;
    [self.view addSubview:self.tableView];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    int num=0;
    switch (section) {
        case 0:
            num=2;
            break;
        case 1:
            num=2;
            break;
        case 2:
            num=3;
            break;
        case 3:
            num=4;
            break;
//            case 4:
//            num=3;
//            break;
            
        default:
            break;
    }
    return num;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    if(indexPath.row==0){
        cell.backgroundColor=cell.contentView.backgroundColor=RGB(232, 233, 232);
    }else{
        if(indexPath.section==0){
        
        UIImageView *icon=[[UIImageView alloc] initWithFrame:CGRectMake(15.f, 13.f, 24.f, 24.f)];
        icon.image=[UIImage imageNamed:@"setting_push_icon.png"];
        [cell addSubview:icon];
        

        UILabel *label=[[UILabel alloc] init];
        label.font=[UIFont systemFontOfSize:15.f];
        label.textColor=RGB(0x54, 0x54, 0x54);
        label.frame=CGRectMake(60.f, (50-label.font.lineHeight)/2.f, GlobleWidth-60.f, label.font.lineHeight);
        label.text=@"推送设置";
        [cell addSubview:label];
        
        UISwitch *sw=[[UISwitch alloc] init];
        sw.frame=CGRectMake(255.f, 10.f, 50, 30);
        [cell addSubview:sw];
        [sw setOn:![CLGlobleDataModel sharedInstance].isNotPush];
        
        __weak UISwitch *sww=sw;
        [sw handleControlEvent:UIControlEventValueChanged withBlock:^(id sender) {
            
            if(sww.on){
                [[CLGlobleDataModel sharedInstance] registPush];
            }else{
               [[CLGlobleDataModel sharedInstance] unRegistPush];
            }
        }];
    }else if(indexPath.section==1){
        UIImageView *icon=[[UIImageView alloc] initWithFrame:CGRectMake(15.f, 13.f, 24.f, 24.f)];
        icon.image=[UIImage imageNamed:@"setting_clear_icon.png"];
        [cell addSubview:icon];
        
        UILabel *label=[[UILabel alloc] init];
        label.font=[UIFont systemFontOfSize:15.f];
        label.textColor=RGB(0x54, 0x54, 0x54);
        label.frame=CGRectMake(60.f, (50-label.font.lineHeight)/2.f, GlobleWidth-60.f, label.font.lineHeight);
        label.text=@"清理缓存";
        [cell addSubview:label];
        UILabel *lable2=[[UILabel alloc] init];
        lable2.font=[UIFont systemFontOfSize:14.f];
        lable2.textAlignment=NSTextAlignmentRight;
        lable2.textColor=RGB(0x54, 0x54, 0x54);
        lable2.frame=CGRectMake(GlobleWidth-115.f, (50-label.font.lineHeight)/2.f, 100, label.font.lineHeight);
        [cell addSubview:lable2];
        self.label2=lable2;
        
        [[SgrGCD sharedInstance] enqueueGloble:^{
            NSUInteger size= [[SDImageCache sharedImageCache] getSize];
            double s=((double)size/1024.0)/1024.f;
            
            [[SgrGCD sharedInstance] enqueueForeground:^{
                NSString *str=[NSString stringWithFormat:@"%.2lfMb",s];
                lable2.text=str;
            }];
        }];
        

    }else if(indexPath.section==2){
        if(indexPath.row==1){
            
            CALayer *line=[CALayer layer];
            line.backgroundColor=RGB(232, 233, 232).CGColor;
            line.frame=CGRectMake(60.f, 49.5f, GlobleWidth-60.f, 0.5);
            [cell.layer addSublayer:line];

            UIImageView *icon=[[UIImageView alloc] initWithFrame:CGRectMake(15.f, 13.f, 24.f, 24.f)];
            icon.image=[UIImage imageNamed:@"setting_font_icon.png"];
            [cell addSubview:icon];
            
            UILabel *label=[[UILabel alloc] init];
            label.font=[UIFont systemFontOfSize:15.f];
            label.textColor=RGB(0x54, 0x54, 0x54);
            label.frame=CGRectMake(60.f, (50-label.font.lineHeight)/2.f, GlobleWidth-60.f, label.font.lineHeight);
            label.text=@"正文字号";
            [cell addSubview:label];
        }else if(indexPath.row==2){
            
            UIImageView *smallA = [[UIImageView alloc]initWithFrame:CGRectMake(14, 32+50, 10, 10)];
            smallA.backgroundColor = [UIColor clearColor];
            [smallA setImage:[UIImage imageNamed:@"set_font_smallA.png"]];
            [cell addSubview:smallA];
            
            UIImageView *bigA = [[UIImageView alloc]initWithFrame:CGRectMake(296, 30.5+50, 15, 15)];
            bigA.backgroundColor = [UIColor clearColor];
            [bigA setImage:[UIImage imageNamed:@"set_font_bigA.png"]];
            [cell addSubview:bigA];
            
            
            UIImageView *imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(40, 35+50, 240, 5.5)];
            imgView1.userInteractionEnabled = YES;
            [imgView1 setImage:[UIImage imageNamed:@"set_font_BG.png"]];
            [cell addSubview:imgView1];
            
            
            self.fontSlider = [[UISlider alloc]initWithFrame:CGRectMake(31, 20+50, 240+11+8, 40.f)];
            self.fontSlider.userInteractionEnabled = YES;
            self.fontSlider.minimumTrackTintColor = [UIColor clearColor];
            self.fontSlider.maximumTrackTintColor = [UIColor clearColor];

            if(1==([INHtmlElementUtil sharedInstance].level)){
                self.fontSlider.value = 0.f;
            }else if(2==([INHtmlElementUtil sharedInstance].level)){
                self.fontSlider.value = 80.f/258.f;
            }else if(3==([INHtmlElementUtil sharedInstance].level)){
                self.fontSlider.value = 169.f/258.f;
            }else if(4==([INHtmlElementUtil sharedInstance].level)){
                self.fontSlider.value = 0.99f;
                
            }
            [cell addSubview:self.fontSlider];
            
            
            UIImage *image = [UIImage imageNamed:@"setting_fontPoint_icon@2x.png"];
            [self.fontSlider setThumbImage:image forState:UIControlStateNormal];
            [self.fontSlider setThumbImage:image forState:UIControlStateHighlighted];
            
            
            //滑块拖动时的事件
            [self.fontSlider addTarget:self action:@selector(sliderValueChanged) forControlEvents:UIControlEventValueChanged];
            //滑动拖动后的事件
            [self.fontSlider addTarget:self action:@selector(sliderDragUp) forControlEvents:UIControlEventTouchUpInside];
            
            
            UIButton *fontBtn1 = [[UIButton alloc]initWithFrame:CGRectMake(31, 29+50, 18, 18)];
            fontBtn1.backgroundColor = [UIColor clearColor];
            fontBtn1.tag = 151;
            [fontBtn1 addTarget:self action:@selector(choseFont:) forControlEvents:UIControlEventTouchUpInside];
//            [cell addSubview:fontBtn1];
            
            UIButton *fontBtn2 = [[UIButton alloc]initWithFrame:CGRectMake(31+(1*80), 29+50, 18, 18)];
            fontBtn2.backgroundColor = [UIColor clearColor];
            fontBtn2.tag = 152;
            [fontBtn2 addTarget:self action:@selector(choseFont:) forControlEvents:UIControlEventTouchUpInside];
//            [cell addSubview:fontBtn2];
            
            UIButton *fontBtn3 = [[UIButton alloc]initWithFrame:CGRectMake(31+(2*80), 29+50, 18, 18)];
            fontBtn3.backgroundColor = [UIColor clearColor];
            fontBtn3.tag = 153;
            [fontBtn3 addTarget:self action:@selector(choseFont:) forControlEvents:UIControlEventTouchUpInside];
//            [cell addSubview:fontBtn3];
            
            UIButton *fontBtn4 = [[UIButton alloc]initWithFrame:CGRectMake(31+(3*80), 29+50, 18, 18)];
            fontBtn4.backgroundColor = [UIColor clearColor];
            fontBtn4.tag = 154;
            [fontBtn4 addTarget:self action:@selector(choseFont:) forControlEvents:UIControlEventTouchUpInside];
//            [cell addSubview:fontBtn4];
            
            
            
        }
    }if(indexPath.section==3){
        if(indexPath.row==1){
            CALayer *line=[CALayer layer];
            line.backgroundColor=RGB(232, 233, 232).CGColor;
            line.frame=CGRectMake(60.f, 49.5f, GlobleWidth-60.f, 0.5);
            [cell.layer addSublayer:line];
            
            UIImageView *icon=[[UIImageView alloc] initWithFrame:CGRectMake(15.f, 13.f, 24.f, 24.f)];
            icon.image=[UIImage imageNamed:@"setting_refresh_icon.png"];
            [cell addSubview:icon];
            
            UILabel *label=[[UILabel alloc] init];
            label.font=[UIFont systemFontOfSize:15.f];
            label.textColor=RGB(0x54, 0x54, 0x54);
            label.frame=CGRectMake(60.f, (50-label.font.lineHeight)/2.f, GlobleWidth-60.f, label.font.lineHeight);
            label.text=@"当前版本";
            [cell addSubview:label];
            
            UILabel *lable2=[[UILabel alloc] init];
            lable2.font=[UIFont systemFontOfSize:14.f];
            lable2.textAlignment=NSTextAlignmentRight;
            lable2.textColor=RGB(0x54, 0x54, 0x54);
            lable2.text = [NSString stringWithFormat:@"V %@",[[CLGloble sharedInstance] appVersion]];
            lable2.frame=CGRectMake(GlobleWidth-115.f, (50-label.font.lineHeight)/2.f, 100, label.font.lineHeight);
            [cell addSubview:lable2];
            
            
        }else if(indexPath.row==2){
            CALayer *line=[CALayer layer];
            line.backgroundColor=RGB(232, 233, 232).CGColor;
            line.frame=CGRectMake(60.f, 49.5f, GlobleWidth-60.f, 0.5);
            [cell.layer addSublayer:line];
            
            UIImageView *icon=[[UIImageView alloc] initWithFrame:CGRectMake(15.f, 13.f, 24.f, 24.f)];
            icon.image=[UIImage imageNamed:@"setting_coverStory_icon.png"];
            [cell addSubview:icon];
            
            UILabel *label=[[UILabel alloc] init];
            label.font=[UIFont systemFontOfSize:15.f];
            label.textColor=RGB(0x54, 0x54, 0x54);
            label.frame=CGRectMake(60.f, (50-label.font.lineHeight)/2.f, GlobleWidth-60.f, label.font.lineHeight);
            label.text=@"封面欣赏";
            [cell addSubview:label];

        }else if(indexPath.row==3){
//            CALayer *line=[CALayer layer];
//            line.backgroundColor=RGB(232, 233, 232).CGColor;
//            line.frame=CGRectMake(60.f, 49.5f, GlobleWidth-60.f, 0.5);
//            [cell.layer addSublayer:line];
            
            UIImageView *icon=[[UIImageView alloc] initWithFrame:CGRectMake(15.f, 13.f, 24.f, 24.f)];
            icon.image=[UIImage imageNamed:@"setting_about_icon.png"];
            [cell addSubview:icon];
            
            UILabel *label=[[UILabel alloc] init];
            label.font=[UIFont systemFontOfSize:15.f];
            label.textColor=RGB(0x54, 0x54, 0x54);
            label.frame=CGRectMake(60.f, (50-label.font.lineHeight)/2.f, GlobleWidth-60.f, label.font.lineHeight);
            label.text=@"关于";
            [cell addSubview:label];

        }
    }
        
    }
    
    return cell;
}

-(void)sliderDragUp{
    
    CGFloat sliderValue = self.fontSlider.value*258.f;
    if (sliderValue>=0.f && sliderValue<=49.f) {
        [INHtmlElementUtil sharedInstance].level=1;
        [[INHtmlElementUtil sharedInstance] buidFont];
        [[IFDocTemplate sharedInstance] changeFont];
        self.fontSlider.value = 0.f;
        
    }else if (sliderValue>49.f && sliderValue<=129.f) {
        [INHtmlElementUtil sharedInstance].level=2;
        [[INHtmlElementUtil sharedInstance] buidFont];
        [[IFDocTemplate sharedInstance] changeFont];
        self.fontSlider.value = 80.f/258.f;
        
    }else if (sliderValue>129.f && sliderValue<=209.f) {
        [INHtmlElementUtil sharedInstance].level=3;
        [[INHtmlElementUtil sharedInstance] buidFont];
        [[IFDocTemplate sharedInstance] changeFont];
        self.fontSlider.value = 169.f/258.f;
        
    }else if (sliderValue>209.f && sliderValue<=258.f) {
        [INHtmlElementUtil sharedInstance].level=4;
        [[INHtmlElementUtil sharedInstance] buidFont];
        [[IFDocTemplate sharedInstance] changeFont];
        self.fontSlider.value = 0.99f;
        
    }
}

-(void)sliderValueChanged{
    
    NSLog(@"sliderValueChanged");
    
    
}

/*
- (void)panFont:(UIPanGestureRecognizer *)rescognizer{
    if(rescognizer.state==UIGestureRecognizerStateEnded || rescognizer.state==UIGestureRecognizerStateCancelled){
        
        
    }else{
       // CGPoint point=[rescognizer locationInView:self.view];
 
    }
}*/

- (void) choseFont:(UIButton *)button {
    switch (button.tag) {
        case 151:
            [INHtmlElementUtil sharedInstance].level=1;
            [[INHtmlElementUtil sharedInstance] buidFont];
            [_fontPointImg removeFromSuperview];
            [button addSubview:_fontPointImg];
            break;
        case 152:
            [INHtmlElementUtil sharedInstance].level=2;
            [[INHtmlElementUtil sharedInstance] buidFont];
            [_fontPointImg removeFromSuperview];
            [button addSubview:_fontPointImg];
            break;
        case 153:
            [INHtmlElementUtil sharedInstance].level=3;
            [[INHtmlElementUtil sharedInstance] buidFont];
            [_fontPointImg removeFromSuperview];
            [button addSubview:_fontPointImg];
            break;
        case 154:
            [INHtmlElementUtil sharedInstance].level=4;
            [[INHtmlElementUtil sharedInstance] buidFont];
            [_fontPointImg removeFromSuperview];
            [button addSubview:_fontPointImg];
            break;
            
        default:
            break;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
        if(indexPath.section==1 && indexPath.row==1){
        if(self.theActive) return;
        __weak typeof(self) me=self;
        [self startLoading];
        [[SgrGCD sharedInstance] enqueueGloble:^{
            [[SDImageCache sharedImageCache] clearMemory];
            [[SDImageCache sharedImageCache] clearDisk];
            sgrSafeMainThread(^{
            me.label2.text=@"0MB";
            })
            
            [[CLGloble sharedInstance] showTieAlertMessage:@"提示" subMessage:@"清理成功"];
            [me stopLoading];
        }];
    }else if(indexPath.section==3 && indexPath.row==1){
        return;
//        [self _check4Update:indexPath];
    }else if(indexPath.section==3 && indexPath.row==2){
                CLSlidersViewController *slider=[CLSlidersViewController new];
                slider.isCover=YES;
                [[CLPushAnimatedRight sharedInstance] pushController:slider];
//        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
//        picker.mailComposeDelegate = self;
//        
//        [picker setSubject:@"意见反馈"];
//        NSArray *toRecipients = [NSArray arrayWithObject:@"chanlinapp@126.com"];
//        
//        
//        [picker setToRecipients:toRecipients];
//        
//         [self presentModalViewController:picker animated:YES];
    }else if(indexPath.section==3 && indexPath.row==3){
//        CLWebVeiwController *web=[CLWebVeiwController new];
//        web.itemId=@"http://www.chanlin.org/iosapp/index.html";
//        [[CLPushAnimatedRight sharedInstance] pushController:web];
        
        CLAboutViewController *about=[CLAboutViewController new];
        [[CLPushAnimatedRight sharedInstance] pushController:about];
    }
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    [controller dismissViewControllerAnimated:YES completion:nil];
    if(result==MFMailComposeResultFailed){
        [[CLGloble sharedInstance] showTieAlertMessage:@"邮箱打开失败" subMessage:@"请检查邮箱配置!"];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    float height=0;
    if(indexPath.row==0){
        height=10;
    }else {
        if (indexPath.section == 2){
            if (indexPath.row == 2) {
                height = 76+50;
            }
        }else
            height=52;
    }
    return height;
}

- (BOOL)showPopAction{
    return YES;
}



@end
